<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>清除宿舍数据</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 10px 0;
            cursor: pointer;
            border-radius: 4px;
        }
        .button-danger {
            background-color: #f44336;
        }
        .logs {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 4px;
            max-height: 300px;
            overflow-y: auto;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>清除宿舍数据工具</h1>
    <p>此工具用于清除localStorage中的宿舍数据，以便重新生成完整的宿舍列表。</p>
    
    <button class="button button-danger" onclick="clearDormitoryData()">清除宿舍数据</button>
    <button class="button" onclick="generateDormitoryData()">重新生成宿舍数据</button>
    <button class="button" onclick="checkData()">查看当前数据</button>
    <button class="button" onclick="clearAllData()">清除所有数据</button>
    
    <div class="logs" id="logs"></div>

    <script>
        const logElement = document.getElementById('logs');
        
        function log(message) {
            const timestamp = new Date().toLocaleTimeString();
            logElement.innerHTML += `<p>[${timestamp}] ${message}</p>`;
            logElement.scrollTop = logElement.scrollHeight;
        }
        
        function clearDormitoryData() {
            if (confirm('确定要清除所有宿舍数据吗？')) {
                localStorage.removeItem('dormitoryStorage');
                log('宿舍数据已清除');
            }
        }
        
        function clearAllData() {
            if (confirm('确定要清除所有数据吗？这将重置整个应用！')) {
                localStorage.clear();
                log('所有数据已清除');
            }
        }
        
        function checkData() {
            const dormData = localStorage.getItem('dormitoryStorage');
            if (dormData) {
                const dorms = JSON.parse(dormData);
                log(`当前共有 ${dorms.length} 个宿舍数据`);
                
                // 获取所有楼栋
                const buildings = [...new Set(dorms.map(d => d.building))];
                log(`楼栋列表: ${buildings.join(', ')}`);
                
                // 统计每个楼栋的宿舍数量
                buildings.forEach(building => {
                    const count = dorms.filter(d => d.building === building).length;
                    log(`- ${building}: ${count} 个宿舍`);
                });
            } else {
                log('没有找到宿舍数据');
            }
        }
        
        function generateDormitoryData() {
            // 生成宿舍数据的函数
            log('开始生成宿舍数据...');
            
            const dormitories = [];
            let dormitoryId = 1;
            
            // 生成随机入住人数的函数
            const getRandomOccupancy = (capacity) => {
                return Math.floor(Math.random() * (capacity + 1));
            };
            
            // 生成创建时间的函数
            const getRandomDate = () => {
                return new Date().toISOString();
            };
            
            // 一号楼男生宿舍
            const building1Floors = [1, 2, 3, 4, 5];
            building1Floors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    const capacity = 4;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '一号楼',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'male',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 二号楼女生宿舍
            const building2Floors = [1, 2, 3, 4, 5];
            building2Floors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    const capacity = 4;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '二号楼',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'female',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 学三舍男生宿舍
            const xuesansheFloors = [1, 2, 3, 4];
            xuesansheFloors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    const capacity = 6;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学三舍',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'male',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 学四舍女生宿舍
            const xuesisheFloors = [1, 2, 3, 4];
            xuesisheFloors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    const capacity = 6;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学四舍',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'female',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 五号楼女生宿舍
            const building5Floors = [1, 2, 3, 4];
            building5Floors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 12; roomNum++) {
                    const capacity = 4;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '五号楼',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'female',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 学五舍男生宿舍
            const xuewusheFloors = [1, 2, 3, 5];
            xuewusheFloors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    const capacity = 6;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学五舍',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'male',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 留学生公寓
            const foreignStudentsFloors = [1, 2, 3];
            foreignStudentsFloors.forEach(floor => {
                for (let roomNum = 1; roomNum <= 6; roomNum++) {
                    const capacity = 2;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '留学生公寓',
                        roomNumber: `${String(roomNum).padStart(3, '0')}`,
                        type: 'mixed',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '混合宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });
            
            // 研究生楼女生部分
            for (let floor = 1; floor <= 2; floor++) {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    const capacity = 2;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '研究生楼',
                        roomNumber: `F${floor}W${String(roomNum).padStart(2, '0')}`,
                        type: 'female',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            }
            
            // 研究生楼男生部分
            for (let floor = 1; floor <= 2; floor++) {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    const capacity = 2;
                    const currentOccupancy = getRandomOccupancy(capacity);
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '研究生楼',
                        roomNumber: `F${floor}M${String(roomNum).padStart(2, '0')}`,
                        type: 'male',
                        capacity: capacity,
                        currentOccupancy: currentOccupancy,
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            }
            
            // 保存到localStorage
            localStorage.setItem('dormitoryStorage', JSON.stringify(dormitories));
            log(`宿舍数据生成完成，共生成 ${dormitories.length} 个宿舍`);
            
            // 显示楼栋统计
            const buildings = [...new Set(dormitories.map(d => d.building))];
            log(`生成的楼栋: ${buildings.join(', ')}`);
        }
        
        // 页面加载时自动检查数据
        window.onload = function() {
            log('页面已加载，点击按钮执行操作');
            checkData();
        };
    </script>
</body>
</html>